package com.qf.smartplatform.utils;

import com.qf.smartplatform.constans.ResultCode;
import com.qf.smartplatform.exceptions.UnLoginException;
import com.qf.smartplatform.pojo.BaseUser;
import com.qf.smartplatform.pojo.SysUserInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SecurityUtils {

    public static BaseUser getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication!=null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof BaseUser) {

                return (BaseUser) principal;
            }
        }
        return null;
    }

//    public static SysUserInfo   getLoginUser(){
//        SysUserInfo user = (SysUserInfo) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("user");
//        Assert.notNull(user,()->{
//            throw new UnLoginException("未登录，请先登录", ResultCode.NOT_LOGIN);
//        });
//        return user;
//    }
}
